<template>
  <div class="wrap_menu">
    <el-form ref="formData" :rules="rules" :model="formData" class="form-wrap" label-position="top" label-width="80px">
      <el-form-item :label="$t('configLazyLoad.process.nodeName')" prop="DisplayName">
        <el-input v-model.trim="formData.DisplayName"></el-input>
      </el-form-item>
      <el-form-item>
        <span style="font-size: 12px;color: #000;margin-right: 16px;">播报的文本：</span>
        <el-radio-group v-model.trim="formData.Type">
          <el-radio label="normal">普通模式</el-radio>
          <el-radio label="mrcp" v-show="platformType === 'ali'">mrcp模式</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="终止语音识别按键：" prop="StopSignal" v-show="formData.Type !== 'mrcp'">
        <el-input v-model.trim="formData.StopSignal"></el-input>
      </el-form-item>
      <el-form-item label="沉默时间(秒)：" prop="SilenceTime" v-show="formData.Type !== 'mrcp'">
        <!-- <el-input v-model="formData.SilenceTime"></el-input> -->
        <el-input v-model.trim="formData.SilenceTime">
          <template slot="append">秒</template>
        </el-input>
      </el-form-item>
      <el-form-item class="wrap_vioce" v-show="formData.Type === 'mrcp'" label="录音前提示音：">
        <el-input v-model.trim="formData.PreVoice"></el-input>
        <span class="voice_tip">如果未设置，则使用默认提示语“您好，请录音”</span>
      </el-form-item>
      <el-form-item>
        <span style="font-size: 12px;color: #000;margin-right: 16px;">是否交给机器人处理：</span>
        <el-radio-group v-model.trim="formData.IsRobotHandle" @change="turnRobot">
          <el-radio :label="true">是</el-radio>
          <el-radio :label="false">否</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item v-show="formData.IsRobotHandle" label="终止机器人问答关键字 (如果需要多个关键字,请用;分隔)：">
        <el-input v-model.trim="formData.EndRobotKey"></el-input>
      </el-form-item>
      <el-form-item label="返回结果赋值变量：" v-show="!formData.IsRobotHandle">
        <el-select v-model.trim="formData.Variable" placeholder="请选择" style="width: 100%;">
          <el-option
            v-for="item in vars.vars"
            :label="item.VarName"
            :key="item._id"
            :value="item._id">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="识别后转向：">
        <node-select-item :curPbx="curPbx" :ToPeerType="formData.NextPeerType" :ToPeer="formData.NextPeer" ref="nodeSelect"></node-select-item>
      </el-form-item>
      <el-form-item label="热词：" v-show="formData.Type !== 'mrcp'">
        <el-select v-model="formData.Vocab_id" placeholder="请选择" style="width: 100%;">
          <el-option
            v-for="item in hodeCodeList"
            :label="item.name"
            :key="item._id"
            :value="item.vocab_id">
          </el-option>
        </el-select>
      </el-form-item>
    </el-form>
    <div class="footer">
      <el-button class='slide-box__button' @click='cancelClick'>
        取消
      </el-button>
      <el-button class='slide-box__button' type="primary" @click='saveClick'>
        保存
      </el-button>
    </div>
  </div>
</template>
<script>
  import nodeSelectItem from '../../base/nodeSelectItem.vue'
  import {deepClone} from 'src/utils/m7Utils.js'
  export default {
    name: 'asrDeal',
    data () {
      let validate1 = (rule, value, callback) => {
        let silenceTimeTest = /^(10|[1-9])$/
        if (!silenceTimeTest.test(value)) {
          callback(new Error(this.$t('沉默时间必须在1-10之间')))
        } else {
          callback()
        }
      }
      return {
        hodeCodeList: [], // 热词数据
        formData: { // 自定义满意度节点
          _id: '',
          DisplayName: '',
          Exten: '',
          Type: 'normal', // 播报的文本
          PreVoice: '', // 录音前提示音
          StopSignal: '#', // 终止语音识别按键
          SilenceTime: '2', // 沉默时间
          Variable: '', // 返回结果赋值变量
          EndRobotKey: '', // 终止机器人问答关键字
          IsRobotHandle: false, // 是否交给机器人处理
          NextPeerType: '', // 识别后转向类型
          NextPeer: '', // 识别后转向值
          Vocab_id: '' // 热词
        },
        rules: {
          DisplayName: [
            { required: true, message: this.$t('configLazyLoad.process.processLabel7'), trigger: 'blur' }
          ],
          SilenceTime: [
            { validator: validate1, required: true, message: '请输入1-10之间数字', trigger: 'blur' }
          ],
          StopSignal: [
            { required: true, message: '请输入终止语音识别按键', trigger: 'blur' }
          ]
        }
      }
    },
    computed: {
      curPbx () {
        return this.$store.state.config.callCenter.currentPBX
      },
      Account () {
        return this.$store.state.session.user.account
      },
      vars () {
        return this.$store.state.session.ivrMap[this.curPbx].data
      },
      platformType () {
        return this.$store.state.platform_urls.platform_type
      }
    },
    components: {
      nodeSelectItem
    },
    props: {
      form: {
        type: Object,
        default: function () {
          return {
            _id: '',
            DisplayName: '',
            Exten: '',
            Type: 'normal', // 播报的文本
            PreVoice: '', // 录音前提示音
            StopSignal: '#', // 终止语音识别按键
            SilenceTime: '2', // 沉默时间
            Variable: '', // 返回结果赋值变量
            EndRobotKey: '', // 终止机器人问答关键字
            IsRobotHandle: false, // 是否交给机器人处理
            NextPeerType: '', // 识别后转向类型
            NextPeer: '', // 识别后转向值
            Vocab_id: '' // 热词
          }
        }
      }
    },
    methods: {
      turnRobot () {
        this.formData.Variable = ''
      },
      // 获取热词数据
      getHotCode () {
        let data = {
          idle: ''
        }
        this.$store.dispatch('queryHotCode', data).then(res => {
          this.hodeCodeList = res.list
        })
      },
      // 初始化form表单
      initForm (obj) {
        this.formData = deepClone(obj)
        this.getHotCode()
      },
      cancelClick () {
        this.$emit('cancelClick')
      },
      // 更新pbx数据
      updateVuexPbx () {
        this.$store.dispatch('queryAsr', {Account: this.Account, PBX: this.curPbx}).then(res => {
          if (res && res.success) {
            this.$store.commit('SET_IVRMAP_DETAIL', {PBX: this.curPbx, typeName: 'ivr_node_asr', data: res.list || [], flag: 'data', node: this})
          }
          this.$emit('saveClick')
        })
      },
      saveClick () {
        this.$refs['formData'].validate((valid) => {
          if (valid) {
            let form = deepClone(this.formData)
            if (!form.id) {
              form.Account = this.Account
              form.PBX = this.curPbx
            }
            form.NextPeerType = this.$refs.nodeSelect.ToPeerTypeThis
            form.NextPeer = this.$refs.nodeSelect.ToPeerThis
            form.UpdateTime && delete form.UpdateTime
            this.$store.dispatch('updateAsr', form).then((res) => {
              if (res.success) {
                this.$message({
                  message: '保存成功',
                  type: 'success'
                })
                this.updateVuexPbx()
              } else {
                this.$message.error('保存失败')
              }
            })
          }
        })
      }
    },
    beforeMount () {
      this.initForm(this.form)
    },
    watch: {
      form (cur, old) {
        this.$refs['formData'].resetFields()
        this.initForm(cur)
        this.$refs.nodeSelect.init()
      }
    }
  }
</script>
<style lang="stylus" scoped>
  @import "../../../../../../assets/common.styl"
  .wrap_vioce
    margin-bottom 20px
    .voice_tip
      color $c-prompt
      font-size 12px
      position absolute
      left 0
      top 26px
  .note
    margin-top 20px
    padding 0 20px
    p
      color $c-prompt
    p.two
      margin-top 10px
  .wrap_menu
    width 100%
    padding-bottom 60px
    .form-wrap
      padding 20px
  .footer
    position absolute
    bottom 0
    left 0
    width 100%
    background rgba(255,255,255,1)
    box-shadow 0px -2px 4px 0px rgba(5,49,38,0.05)
    border-top 1px solid rgba(232,232,232,1)
    display flex
    justify-content center
    padding 13px 0
    box-sizing border-box
    .slide-box__button
      padding 8px 20px 8px
      box-sizing border-box
</style>
